package com.example.mystore.controller;

import com.example.mystore.model.Product;
import com.example.mystore.model.Category;
import com.example.mystore.service.ProductService;
import com.example.mystore.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/api/products")
public class ProductController {


    @Autowired
    private ProductService productService;

    // 获取推荐商品
    @GetMapping("/featured")
    public List<Product> getFeaturedProducts() {
        return productService.getFeaturedProducts();
    }

    @Autowired
    private CategoryService categoryService;

    // 获取所有商品
    @GetMapping
    public List<Product> getAllProducts() {
        return productService.getAllProducts();
    }

    // 获取指定商品
    @GetMapping("/{id}")
    public Product getProductById(@PathVariable Long id) {
        return productService.getProductById(id);
    }

    // 添加商品
    @PostMapping
    public ResponseEntity<?> addProduct(@RequestParam String name, @RequestParam double price,
                                        @RequestParam int stock, @RequestParam Long category,
                                        @RequestParam MultipartFile image, @RequestParam String description) {
        Category cat = categoryService.getAllCategories().stream()
                .filter(c -> c.getId().equals(category))
                .findFirst()
                .orElse(null);
        if (cat == null) {
            return ResponseEntity.status(400).body("分类不存在");
        }

        productService.addProduct(name, price, stock, category, image, description);
        return ResponseEntity.ok().body("商品添加成功");
    }

    // 更新商品
    @PutMapping("/{id}")
    public ResponseEntity<?> updateProduct(@PathVariable Long id, @RequestParam String name,
                                           @RequestParam double price, @RequestParam int stock,
                                           @RequestParam Long category, @RequestParam MultipartFile image,
                                           @RequestParam String description) {
        Category cat = categoryService.getAllCategories().stream()
                .filter(c -> c.getId().equals(category))
                .findFirst()
                .orElse(null);
        if (cat == null) {
            return ResponseEntity.status(400).body("分类不存在");
        }

        productService.updateProduct(id, name, price, stock, category, image, description);
        return ResponseEntity.ok().body("商品更新成功");
    }

    // 删除商品
    @DeleteMapping("/{id}")
    public ResponseEntity<?> deleteProduct(@PathVariable Long id) {
        productService.deleteProduct(id);
        return ResponseEntity.ok().body("商品删除成功");
    }

    // 获取分类下的商品
    @GetMapping("/category/{categoryId}")
    public List<Product> getProductsByCategory(@PathVariable Long categoryId) {
        return productService.getProductsByCategory(categoryId);
    }
}
